package com.mate.cloud.auth.handler;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 自定义登陆成功处理逻辑
 *
 * @author: MI
 * @email: 448341911@qq.com
 * @createTime: 2025/6/5 16:54
 * @updateUser: MI
 * @updateTime: 2025/6/5 16:54
 * @updateRemark: 修改内容
 * @version: 1.0
 */
@Slf4j
@Component
public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        System.out.println("onAuthenticationSuccess()");
        // 这里可以添加自定义逻辑，例如记录登录时间、根据用户角色跳转
        String username = authentication.getName();
        System.out.println("用户 " + username + " 登录成功");


        // 重置登录尝试次数
        CustomAuthenticationFailureHandler2.resetAttempts(username);

        // 默认重定向到首页，可根据需求修改
        response.sendRedirect(request.getContextPath() + "/success");
    }
}
